const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const Resume = sequelize.define('Resume', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  link_token: {
    type: DataTypes.STRING,
    allowNull: false,
    field: 'link_token',
    comment: '链接令牌，用于标识简历来源'
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '姓名'
  },
  nickname: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '昵称'
  },
  phone: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: '手机号码'
  },
  wechat: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '微信号'
  },
  gender: {
    type: DataTypes.ENUM('male', 'female', 'other'),
    allowNull: false,
    comment: '性别：男(male)、女(female)、其他(other)'
  },
  age: {
    type: DataTypes.INTEGER,
    allowNull: true,
    validate: {
      min: 16,
      max: 100
    },
    comment: '年龄'
  },
  weight: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    validate: {
      min: 30,
      max: 200
    },
    comment: '体重(kg)'
  },
  education: {
    type: DataTypes.ENUM('高中/中专', '大专', '本科', '硕士', '博士', '其他'),
    allowNull: true,
    comment: '学历'
  },
  hobbies: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '兴趣爱好'
  },
  special_skills: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'special_skills',
    comment: '才艺展示'
  },
  can_drink: {
    type: DataTypes.ENUM('yes', 'no'),
    allowNull: false,
    field: 'can_drink',
    comment: '可否饮酒：是(yes)、否(no)'
  },
  can_livestream: {
    type: DataTypes.ENUM('yes', 'no'),
    allowNull: false,
    field: 'can_livestream',
    comment: '能否配合直播宣传：是(yes)、否(no)'
  },
  accept_night_shift: {
    type: DataTypes.ENUM('yes', 'no'),
    allowNull: false,
    defaultValue: 'no',
    field: 'accept_night_shift',
    comment: '是否能接受夜班：是(yes)、否(no)'
  },
  preferred_shift: {
    type: DataTypes.ENUM('day', 'night'),
    allowNull: false,
    field: 'preferred_shift',
    comment: '期望工作时间：早班(day)、晚班(night)'
  },
  game_experience: {
    type: DataTypes.ENUM('none', 'basic', 'familiar', 'expert'),
    allowNull: false,
    field: 'game_experience',
    comment: '桌游与Switch熟悉程度：完全不了解(none)、略有了解(basic)、比较熟悉(familiar)、非常熟悉(expert)'
  },
  self_introduction: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'self_introduction',
    comment: '自我介绍'
  },
  photos: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '照片URL数组，JSON格式',
    validate: {
      isValidPhotoArray(value) {
        if (value && (!Array.isArray(value) || value.length === 0 || value.length > 5)) {
          throw new Error('照片数量必须在1-5张之间');
        }
      }
    }
  },
  work_experience: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'work_experience',
    comment: '工作经历，JSON格式，包含startDate、endDate、company、position、reasonForLeaving字段'
  },
  weekly_availability: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'weekly_availability',
    comment: '每周空闲时间，JSON格式'
  },
  status: {
    type: DataTypes.ENUM('pending', 'approved', 'rejected'),
    allowNull: true,
    defaultValue: 'pending',
    comment: '状态：待审核(pending)、已通过(approved)、已拒绝(rejected)'
  },
  invitation_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'invitation_id',
    comment: '关联的邀请ID'
  }
}, {
  tableName: 'resumes',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  underscored: true,
  indexes: [
    {
      name: 'idx_status',
      fields: ['status']
    },
    {
      name: 'idx_created',
      fields: ['created_at']
    },
    {
      name: 'idx_invitation_id',
      fields: ['invitation_id']
    }
  ]
});

module.exports = Resume; 